#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "esp_log.h"

#include "driver/touch_pad.h"
#include "soc/rtc_periph.h"
#include "soc/sens_periph.h"
#include "hal_touch.h"



uint16_t initVal;
void touch_sensor_init()
{
    uint16_t touch_value;
    // Initialize touch pad peripheral.
    // The default fsm mode is software trigger mode.
    touch_pad_init();
    // Set reference voltage for charging/discharging
    // In this case, the high reference valtage will be 2.7V - 1V = 1.7V
    // The low reference voltage will be 0.5
    // The larger the range, the larger the pulse count value.
    touch_pad_set_voltage(TOUCH_HVOLT_2V7, TOUCH_LVOLT_0V5, TOUCH_HVOLT_ATTEN_1V);
    touch_pad_config(TOUCH_PAD_NUM_USER, 0);
    touch_pad_filter_start(10);

    touch_pad_read_filtered(TOUCH_PAD_NUM_USER, &touch_value);
    initVal = touch_value;
}

unsigned int touch_sensor_get_init_val()
{
    return initVal;
}

unsigned int touch_sensor_read()
{
    uint16_t touch_value;
    touch_pad_read_filtered(TOUCH_PAD_NUM_USER, &touch_value);
    return touch_value;
}

